Date and Time in R

R4DS 13 - Lubridate

lruolin
05-24-2021

R4DS Practice 13: lubridate

The codes below are from the practice exercises in https://r4ds.had.co.nz/, and are taken with reference from: https://jrnold.github.io/r4ds-exercise-solutions/

Let’s begin now

Loading tidyverse package.

Creating Date/Times from Strings

Identify the order in which the year, month and day appear in your dates, then arrange y, m , d in the same order.

ymd("2021-05-24")
[1] "2021-05-24"
# you can also use unquoted numbers

ymd(20210524)
[1] "2021-05-24"
# you can also add time using h, m, s

ymd_hms ("2021-05-24 20:11:09", tz = "Singapore")
[1] "2021-05-24 20:11:09 +08"
# Use OlsonNames() to display timezone names

Creating Dates from Individual Components

Sometimes, you have the individual components instead of teh date-time spread across multiple columns.

flights %>% 
  select(year, month, day, hour, minute)
# A tibble: 336,776 x 5
    year month   day  hour minute
   <int> <int> <int> <dbl>  <dbl>
 1  2013     1     1     5     15
 2  2013     1     1     5     29
 3  2013     1     1     5     40
 4  2013     1     1     5     45
 5  2013     1     1     6      0
 6  2013     1     1     5     58
 7  2013     1     1     6      0
 8  2013     1     1     6      0
 9  2013     1     1     6      0
10  2013     1     1     6      0
# … with 336,766 more rows
# To create date/time:
# use make_date()
# or make_datetime()

flights %>% 
  select(year, month, day, hour, minute) %>% 
  mutate(departure = make_datetime(year, month, day, hour, minute))
# A tibble: 336,776 x 6
    year month   day  hour minute departure          
   <int> <int> <int> <dbl>  <dbl> <dttm>             
 1  2013     1     1     5     15 2013-01-01 05:15:00
 2  2013     1     1     5     29 2013-01-01 05:29:00
 3  2013     1     1     5     40 2013-01-01 05:40:00
 4  2013     1     1     5     45 2013-01-01 05:45:00
 5  2013     1     1     6      0 2013-01-01 06:00:00
 6  2013     1     1     5     58 2013-01-01 05:58:00
 7  2013     1     1     6      0 2013-01-01 06:00:00
 8  2013     1     1     6      0 2013-01-01 06:00:00
 9  2013     1     1     6      0 2013-01-01 06:00:00
10  2013     1     1     6      0 2013-01-01 06:00:00
# … with 336,766 more rows

Use the appropriate lubridate function to parse each of the following dates:

d1 <- "January 1, 2010"
mdy(d1)
[1] "2010-01-01"
d2 <- "2015-Mar-07"
ymd(d2)
[1] "2015-03-07"
d3 <- "06-Jun-2017"
dmy(d3)
[1] "2017-06-06"
d4 <- c("August 19 (2015)", "July 1 (2015)")
mdy(d4)
[1] "2015-08-19" "2015-07-01"
d5 <- c("12/30/14") # Dec 30, 2014
mdy(d5)
[1] "2014-12-30"

Reference

https://r4ds.had.co.nz/

https://jrnold.github.io/r4ds-exercise-solutions/

Citation

For attribution, please cite this work as

lruolin (2021, May 24). pRactice corner: Date and Time in R. Retrieved from https://lruolin.github.io/myBlog/posts/20210524_Tidyverse Chap 13 - Lubridate/

BibTeX citation

@misc{lruolin2021date,
  author = {lruolin, },
  title = {pRactice corner: Date and Time in R},
  url = {https://lruolin.github.io/myBlog/posts/20210524_Tidyverse Chap 13 - Lubridate/},
  year = {2021}
}